﻿@model MvcWeb.ViewModels.Role.RoleModel
@{
    ViewBag.Title = "角色管理";
}
<style type="text/css">
    label { margin-right: 10px; }
</style>
<div>
    <div style="padding: 5px 10px 0px 10px;">
        <input type="button" value="新建角色" class="btn_green" onclick="window.location='/role'" />
        <input type="button" value="删除角色" class="btn_red" onclick="deleteRole()" />
    </div>
    <div style="clear: both;">
    </div>
    <div style="width: 200px; float: left; padding: 10px;">
        <table class="tb" width="98%">
            <tr>
                <th width="20">
                    <input type="checkbox" onclick="$(this).parent().parent().parent().find(':checkbox').attr('checked', this.checked)" />
                </th>
                <th>角色名称</th>
                <th width="40">操作</th>
            </tr>
            @foreach (var m in Model.aspnet_Roles)
            {
                <tr>
                    <td style="text-align: center;">
                        <input type="checkbox" name="rid" value="@m.RoleId" /></td>
                    <td>@m.RoleName</td>
                    <td style="text-align: center;">
                        <a href="@Url.Action("index", new { id = m.RoleId })">编辑</a></td>
                </tr>   
            }
        </table>
    </div>
    <div style="margin-left: 220px; padding: 10px;">
        @using (Html.BeginForm())
        {
            <div>
                <table class="tb">
                    <tr>
                        <td>@Html.LabelFor(m => m.RoleName)</td>
                        <td>
                            @Html.TextBoxFor(m => m.RoleName)
                            @Html.ValidationMessageFor(m => m.RoleName)
                            @Html.HiddenFor(m => m.Privileges)
                        </td>
                    </tr>
                    <tr>
                        <td>所有模块</td>
                        <td>
                            <ul id="maintree">
                            </ul>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <input type="submit" value="保存角色" class="btn_green" />
                            <input type="reset" value="重置" class="btn_orange" />
                        </td>
                    </tr>
                </table>
            </div>
        }
    </div>
</div>
<script type="text/javascript">
    var tree = null, node = null;

    $(function () {
        tree = $("#maintree").ligerTree({
            url: '/module/root?t=' + Math.random(),
            onCheck: getModule,
            onSuccess: setChecked
        });
    });

    function setChecked() {
        var parm = function (data) {
            var arr = $("#Privileges").val().split(',');
            for (var i = 0, n = arr.length; i < n; i++) {
                if (parseInt(arr[i]) > 0 && data.id == arr[i]) {
                    return true;
                }
            }
            return false;
        };

        tree.selectNode(parm);
    }

    function deleteRole() {
        if (!confirm('确定删除吗？')) return;
        if ($("input[name='rid']:checked").length == 0) return;

        var arr = [];
        $("input[name='rid']:checked").each(function () {
            arr.push($(this).val());
        });

        $.post("/role/delete", { ids: arr.join(',') }, function () {
            window.location = window.location;
        });
    }

    function getModule() {
        var notes = tree.getChecked();
        var ids = [];
        for (var i = 0, n = notes.length; i < n; i++) {
            if (parseInt(notes[i].data.id) > 0)
                ids.push(notes[i].data.id);
        }
        $("#Privileges").val(ids.join(','));
    }
</script>
